<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
class Index extends Controller
{
    public function index()
    {
        $list=Db('diqu')->select();
        $res=db('logs')->insert(["name"=>"展示地区列表"]);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function guanggaowei()
    {
    	 $list=Db('guanggaowei')->select();
        $this->assign('list',$list);
        $res=db('logs')->insert(["name"=>"展示广告位列表"]);
        return $this->fetch();
    }
    public function guanggaotu()
    {
    	$id=input('id');
        $list=Db('guanggaotu')->where('guanggao_id',$id)->select();
        $this->assign('list',$list);
        $this->assign('id',$id);
        $res=db('logs')->insert(["name"=>"展示广告图列表"]);
        return $this->fetch();
    }
    public function guanggaotu_add()
    {
    	$id=input('id');
    	if($this->request->isPost()){
    		$data=input();
    		unset($data['file'],$data['/index/index/guanggaotu_add_html']);
    	    $res=db('guanggaotu')->insert($data);
    	    if($res){
    	    	$reds=db('logs')->insert(["name"=>"添加广告图"]);
    	    	return $this->success("添加成功",url('index/guanggaotu',['id'=>$data['guanggao_id']]));
    	    }else{
    	    	return $this->error("添加失败");
    	    }
    		
    	}else{
    		$list=Db('diqu')->where('pid',0)->select();
    	    $this->assign('list',$list);
            $this->assign('id',$id);
            return $this->fetch();
    	}
    	
    }
    public function guanggaotu_delete()
    {
    	$id=input('id');
    	$res=db('guanggaotu')->where('id',$id)->delete();
    	if($res){
    		$red=db('logs')->insert(["name"=>"删除广告图"]);
    		return $this->success("删除成功");
    	}else{
    		return $this->error("添加失败");
    	}
    }
    public function guanggaotu_edit()
    {
    	$id=input('id');
    	if($this->request->isPost()){
    		$data=input();
    		unset($data['file'],$data['/index/index/guanggaotu_add_html']);
    		
    	    $res=db('guanggaotu')->where('id',$data['id'])->update($data);
    	    if($res){
    	    	$reds=db('logs')->insert(["name"=>"编辑广告图"]);
    	    	return $this->success("编辑成功",url('index/guanggaotu',['id'=>$data['guanggao_id']]));
    	    }else{
    	    	return $this->error("编辑失败");
    	    }
    		
    	}else{
    		$data=Db('guanggaotu')->where('id',$id)->find();
    	    $list=Db('diqu')->where('pid',0)->select();
    	    $pid=Db('diqu')->where('name',$data['sheng'])->value('id');
         	$shi=db('diqu')->where('pid',$pid)->select();
    	    $qu_id=Db('diqu')->where('name',$data['shi'])->value('id');
        	$qu=db('diqu')->where('pid',$qu_id)->select();
    	    $this->assign('list',$list);
    	    $this->assign('data',$data);
    	    $this->assign('shi',$shi);
    	    $this->assign('qu',$qu);
           return $this->fetch();
    	}
    	
    }
    public function upload()
    {
    	$file = request()->file('file');
    	$info = $file->move('./public/upload');
    	if($info){
    		$image = \think\Image::open($info);
            $image->thumb(150, 150)->save('./public/upload/'.$info->getsaveName());
            $path='http://wwj.com/'.'public/upload/'.$info->getsaveName();
            return json(["code"=>0,"data"=>$path]);
    	}else{
    		  return json(["code"=>1,"data"=>'']);
    	}
    }
    public function diqu()
    {
    	$name=input('name');
    	$id=Db('diqu')->where('name',$name)->value('id');
    	$list=db('diqu')->where('pid',$id)->select();
    	if($list){
    		return  json(["code"=>0,"msg"=>"成功","data"=>$list]);
    	}else{
    		return  json(["code"=>1,"msg"=>"失败","data"=>'']);
    	}
    }
}
